import os
import logging

from utils.error import MyError, SimpleError

logging.basicConfig(level=os.getenv("LOGLEVEL", "info".upper()),
                    format='%(asctime)s - %(levelname)s [%(process)d -> %(thread)d] - %(message)s')


class Log(object):
    def debug(self, msg):
        logging.debug(f"{msg}")

    def info(self, msg):
        logging.info(f"{msg}")

    def warning(self, msg):
        logging.warning(f"{msg}")

    def error(self, code, msg, detail=None, end=False):
        msg_s = f"code {code} msg {msg}"
        if detail is not None:
            msg_s = f"{msg_s} detail {detail}"
        logging.error(msg_s)
        err = MyError(code, msg, detail)
        if end:
            return err
        else:
            raise err

    def simple_error(self, msg, end=False):
        logging.error(msg)
        err = SimpleError(msg)
        if end:
            return err
        else:
            raise err
